home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / fat_write.c < prev    next >
C/C++ Source or Header  |  1992-09-10  |  3KB  |  97 lines

  1. #include <stdio.h>
  2. #include "msdos.h"
  3.  
  4. extern int fd, dir_start, num_fat, fat_len, fat_error, fat_bits;
  5. extern unsigned char *fat_buf;
  6.  
  7. /*
  8.  * Puts a code into the FAT table.  Is the opposite of fat_decode().  No
  9.  * sanity checking is done on the code.  Returns a 1 on error.
  10.  */
  11.  
  12. int
  13. fat_encode(num, code)
  14. unsigned int num;
  15. unsigned int code;
  16. {
  17.     unsigned int start;
  18.  
  19.     if (fat_bits == 12) {
  20.         /*
  21.          *    |    byte n     |   byte n+1    |   byte n+2    |
  22.          *    |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
  23.          *    | | | | | | | | | | | | | | | | | | | | | | | | |
  24.          *    | n+0.0 | n+0.5 | n+1.0 | n+1.5 | n+2.0 | n+2.5 |
  25.          *        \_____  \____   \______/________/_____   /
  26.          *          ____\______\________/   _____/  ____\_/
  27.          *         /     \      \          /       /     \
  28.          *    | n+1.5 | n+0.0 | n+0.5 | n+2.0 | n+2.5 | n+1.0 |
  29.          *    |      FAT entry k      |    FAT entry k+1      |
  30.          */
  31.                     /* which bytes contain the entry */
  32.         start = num * 3 / 2;
  33.         if (start <= 2 || start + 1 > (fat_len * MSECTOR_SIZE))
  34.             return(1);
  35.                     /* (odd) not on byte boundary */
  36.         if (num % 2) {
  37.             *(fat_buf + start) = (*(fat_buf + start) & 0x0f) + ((code << 4) & 0xf0);
  38.             *(fat_buf + start + 1) = (code >> 4) & 0xff;
  39.         }
  40.                     /* (even) on byte boundary */
  41.         else {
  42.             *(fat_buf + start) = code & 0xff;
  43.             *(fat_buf + start + 1) = (*(fat_buf + start + 1) & 0xf0) + ((code >> 8) & 0x0f);
  44.         }
  45.     }
  46.     else {
  47.         /*
  48.          *    |    byte n     |   byte n+1    |
  49.          *    |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
  50.          *    | | | | | | | | | | | | | | | | |
  51.          *    |         FAT entry k           |
  52.          */
  53.                     /* which bytes contain the entry */
  54.         start = num * 2;
  55.         if (start <= 3 || start + 1 > (fat_len * MSECTOR_SIZE))
  56.             return(1);
  57.  
  58.         *(fat_buf + start + 1) =  code / 0x100;
  59.         *(fat_buf + start) =  code % 0x100;
  60.     }
  61.     return(0);
  62. }
  63.  
  64. /*
  65.  * Write the FAT table to the disk.  Up to now the FAT manipulation has
  66.  * been done in memory.  All errors are fatal.  (Might not be too smart
  67.  * to wait till the end of the program to write the table.  Oh well...)
  68.  */
  69.  
  70. void
  71. fat_write()
  72. {
  73.     int i, start, dups;
  74.     void disk_write();
  75.  
  76.     if (fd < 0)
  77.         return;
  78.  
  79.     start = dir_start - (fat_len * num_fat);
  80.  
  81.     for (i=start; i<start+fat_len; i++)
  82.         disk_write((long) i, &fat_buf[(i-start)*MSECTOR_SIZE], MSECTOR_SIZE);
  83.  
  84.     /*
  85.      * Only duplicate the FAT table if no errors were detected
  86.      */
  87.     if (!fat_error) {
  88.         dups = num_fat - 1;
  89.         while (dups--) {
  90.             start += fat_len;
  91.             for (i=start; i<start+fat_len; i++)
  92.                 disk_write((long) i, &fat_buf[(i-start)*MSECTOR_SIZE], MSECTOR_SIZE);
  93.         }
  94.     }
  95.     return;
  96. }
  97.